package cn.javabb.core.base;

import cn.javabb.core.entity.sys.UserDO;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

public abstract class BaseController {

	@Autowired
	protected HttpServletRequest request;
	
	protected String page;
	
	protected String size;
	
	/**
	 * 
	 * @author QINB
	 * @return
	 */
    protected String getPage() {
        page = request.getParameter("page");
        if(null==page||"".equals(page)){
            page = "1";
        }
        return page;
    }


    protected String getSize() {
        size = request.getParameter("size");
        if(StringUtils.isBlank(size)){
            size = request.getParameter("limit");
        }
        if(null==size||"".equals(size)){
            size = "10";
        }
        return size;
    }
    protected String getParamValue(String param){
        return request.getParameter(param);
    }
    protected JSONObject getPageSize(){
        String page = getPage();
        String size = getSize();
        JSONObject param = new JSONObject();
        param.put("page", page);
        param.put("size", size);
        return param;
    }
    /**
     * 获取当前登录的user
     */
    public UserDO getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object object = subject.getPrincipal();
            if (object != null) {
                return (UserDO) object;
            }
        }
        return null;
    }
    public ModelAndView error(int errorCode) {
        return new ModelAndView("error/"+errorCode);
    }
    public ModelAndView forward(String view) {
        return new ModelAndView("forward:" + view);
    }

}
